"use strict";
exports.__esModule = true;
var taro_1 = require("@tarojs/taro");
var coreService = {};
function set(key, val) {
    coreService[key] = val;
}
exports.set = set;
function get(key) {
    return coreService[key];
}
exports.get = get;
function del(key) {
    delete coreService[key];
}
exports.del = del;
function compareTimeTypeOne(elementArray) {
    for (var i = 0; i < elementArray.length - 1; i++) {
        if (elementArray[i].showTime) {
            if (new Date().getTime() < new Date(elementArray[i].showTime.replace(/-/g, '/')).getTime()) {
                elementArray.splice(i, 1);
            }
        }
    }
}
exports.compareTimeTypeOne = compareTimeTypeOne;
function compareTimeTypeAll(elementModel) {
    if (elementModel.showTime && new Date().getTime() < new Date(elementModel.showTime.replace(/-/g, '/')).getTime()) {
        elementModel.hidden = true;
    }
}
exports.compareTimeTypeAll = compareTimeTypeAll;
function getWebClass() {
    if (taro_1["default"].getEnv() == taro_1["default"].ENV_TYPE.WEB) {
        return 'web-margin-bottom-bar';
    }
    else {
        return '';
    }
}
exports.getWebClass = getWebClass;
function getTheme() {
    // if (Taro.getEnv() == Taro.ENV_TYPE.WEAPP) {
    //   let config: any = Taro.getExtConfigSync()
    //   if (config && config.theme) {
    //     return config.theme;
    //   }
    //   return THEME;
    // } else {
    //   return THEME;
    // }
    return "";
}
exports.getTheme = getTheme;
function getThemeColor() {
    var config = taro_1["default"].getExtConfigSync();
    if (config && config.themeColor) {
        return config.themeColor;
    }
    return THEME_COLOR;
}
exports.getThemeColor = getThemeColor;
function getStoreId() {
    if (taro_1["default"].getEnv() == taro_1["default"].ENV_TYPE.WEAPP) {
        var config = taro_1["default"].getExtConfigSync();
        if (config && config.storeId) {
            return config.storeId;
        }
        if (typeof STORE_ID != 'undefined') {
            return STORE_ID;
        }
        return 0;
    }
    else {
        return 0;
    }
}
exports.getStoreId = getStoreId;
//获取参数
function getQueryVariable(variable, scene) {
    var query = scene ? decodeURIComponent(scene) : this.$router.params.query;
    if (scene) {
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }
    else {
        return query[variable];
    }
    return "";
}
exports.getQueryVariable = getQueryVariable;
function formatLastTime(date) {
    var nowTime = new Date().getTime();
    date = date.substring(0, 19);
    date = date.replace(/-/g, '/');
    var time = new Date(date).getTime();
    var resTime = (nowTime - time) / 1000;
    var hour48 = 48 * 60 * 60;
    // let day = Math.floor(resTime / (24 * 60 * 60));
    var hour = Math.floor((hour48 - resTime) / (60 * 60));
    var minute = Math.floor((hour48 - resTime) / (60));
    var second = Math.floor((hour48 - resTime));
    var returnStr = { value: "", unit: "" };
    if (resTime >= hour48) {
        returnStr.value = "0";
        return returnStr;
    }
    // if (day > 0) {
    //   returnStr = day.toString();
    //   return returnStr;
    // }
    if (hour > 0) {
        returnStr.value = hour.toString();
        returnStr.unit = "小时";
        return returnStr;
    }
    if (minute > 0) {
        returnStr.value = minute.toString();
        returnStr.unit = "分钟";
        return returnStr;
    }
    if (second > 0) {
        returnStr.value = second.toString();
        returnStr.unit = "秒";
        return returnStr;
    }
    return returnStr;
}
exports.formatLastTime = formatLastTime;
function throttle(fn, gapTime) {
    if (gapTime === void 0) { gapTime = 1500; }
    // if (gapTime == null || gapTime === undefined) {
    //   gapTime = 1500
    // }
    var _lastTime = 0;
    return function () {
        var _nowTime = +new Date();
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            // 将this和参数传给原函数
            fn.apply(this, arguments);
            _lastTime = _nowTime;
        }
    };
}
exports.throttle = throttle;
/**
   * 解析参数或场景值中的参数
   */
function praseParams($router) {
    var query = $router.params.query || $router.params;
    var shopId = 0;
    var goodsId = 0;
    var inviteCustomerId = 0;
    if (query && query.shopId) {
        shopId = query.shopId;
    }
    if (query && query.goodsId) {
        goodsId = query.goodsId;
    }
    if (query && query.customerId) {
        inviteCustomerId = query.customerId;
    }
    var scene = (query && query.scene) || $router.params.scene;
    if (scene) {
        var options = decodeURIComponent(scene).split('&');
        for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
            var option = options_1[_i];
            var keyValue = option.split("=");
            if (keyValue[0] == 'shopId') {
                shopId = +keyValue[1];
            }
            else if (keyValue[0] == 'goodsId') {
                goodsId = +keyValue[1];
            }
            else if (keyValue[0] == 'customerId') {
                inviteCustomerId = +keyValue[1];
            }
        }
    }
    return [shopId, goodsId, inviteCustomerId];
}
exports.praseParams = praseParams;
